package com.dyoud.api.shop.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.dyoud.api.ReturnInfo;
import com.dyoud.api.shop.employee.dto.AppEmployeeInfo;
import com.dyoud.api.shop.employee.dto.AppEmployeeLoginDto;
import com.dyoud.api.shop.employee.dto.EmployeeDto;
import com.dyoud.api.shop.employee.dto.EmployeePageQueryDto;
import com.dyoud.api.shop.service.EmployeeApiService;
import com.dyoud.api.shop.service.ShopApiService;
import com.dyoud.core.ResponseBean;
import com.dyoud.core.code.RedisSmsManager;
import com.dyoud.core.enums.SmsTypeEnum;
import com.dyoud.shop.api.enums.StateEnum;
import com.dyoud.shop.api.model.ShopInfo;
import com.dyoud.shop.api.query.EmployeePage;
import com.github.pagehelper.PageInfo;

/***
 * 
 * @author wangshuaifei
 * @date 2018/03/03
 */
@RestController
@RequestMapping("employee")
public class EmployeeController {

    @Value("${is_dev}")
    private Boolean isDev;

    @Resource
    private EmployeeApiService employeeApiService;
    @Resource
    private ShopApiService shopApiService;

    @Resource
    private RedisSmsManager smsManager;

    /***
     * 添加员工
     * 
     * @param request
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResponseBean addEmployee(@RequestBody @Validated EmployeeDto dto) {
        boolean isOK = employeeApiService.addEmployee(dto);
        if (!isOK) {
            return new ResponseBean().fail(ReturnInfo.ADD_EMPLOYEE_CODE, ReturnInfo.ADD_EMPLOYEE_MESSAGE);
        }
        return new ResponseBean().success();
    }

    /***
     * 修改员工信息
     * 
     * @param request
     * @return
     */
    @RequestMapping(value = "/{employeeId}", method = RequestMethod.PUT)
    public ResponseBean updateEmployee(@PathVariable("employeeId") Long employeeId, @RequestBody @Validated EmployeeDto dto) {
        if (employeeId == null) {
            return new ResponseBean().fail(ReturnInfo.EMPLOYEE_ID_NULL_CODE, ReturnInfo.EMPLOYEE_ID_NULL_MESSAGE);
        }
        AppEmployeeInfo newdto = employeeApiService.getEmployeeByEmployeeId(employeeId);
        if (newdto.getEmployeeId() == null) {
            return new ResponseBean().fail(ReturnInfo.GET_EMPLOYEE_CODE, ReturnInfo.GET_EMPLOYEE_MESSAGE);
        }
        dto.setEmployeeId(employeeId);
        // 判断前台是否传值，如果没有，默认员工为启用状态
        if (dto.getIsEnable() == null) {
            dto.setIsEnable(0);
        }
        boolean b = employeeApiService.updateEmployee(dto);
        if (!b) {
            return new ResponseBean().fail(ReturnInfo.UPDATE_EMPLOYEE_CODE, ReturnInfo.UPDATE_EMPLOYEE_MESSAGE);
        }
        return new ResponseBean().success();
    }

    /***
     * 删除员工，软删除，修改状态isdel为1
     * 
     * @param employeeId
     * @return
     */
    @RequestMapping(value = "/{employeeId}", method = RequestMethod.DELETE)
    public ResponseBean deleteEmployee(@PathVariable("employeeId") Long employeeId) {
        if (employeeId == null) {
            return new ResponseBean().fail(ReturnInfo.EMPLOYEE_ID_NULL_CODE, ReturnInfo.EMPLOYEE_ID_NULL_MESSAGE);
        }
        boolean b = employeeApiService.delEmployeeByEmployeeId(employeeId);
        if (!b) {
            return new ResponseBean().fail(ReturnInfo.DELETE_EMPLOYEE_CODE, ReturnInfo.DELETE_EMPLOYEE_MESSAGE);
        }
        return new ResponseBean().success();
    }

    /***
     * 获取单个员工信息
     * 
     * @param employeeId
     * @return
     */
    @RequestMapping(value = "/{employeeId}/{shopId}", method = RequestMethod.GET)
    public ResponseBean getEmployeeByEmployeeId(@PathVariable("employeeId") Long employeeId, @PathVariable("shopId") Long shopId) {
        AppEmployeeInfo dto = employeeApiService.getEmployeeByEmployeeId(employeeId);
        if (dto == null) {
            return new ResponseBean().fail(ReturnInfo.GET_EMPLOYEE_CODE, ReturnInfo.GET_EMPLOYEE_MESSAGE);
        }
        int count = employeeApiService.countEmployeeCount(employeeId, shopId);
        int personSum = employeeApiService.countEmployeePerson(employeeId, shopId);
        ShopInfo shopInfo = shopApiService.getByShopId(shopId);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("userName", dto.getName());
        result.put("shopName", shopInfo.getName());
        result.put("type", dto.getType());
        result.put("phone", dto.getPhone());
        result.put("count", count);
        result.put("personSum", personSum);
        result.put("logo", shopInfo.getLogo());
        return new ResponseBean().success(result);
    }

    /**
     * 分页查询员工，根据店铺id,员工名称,员工是否被禁用的状态
     * 
     * @param query
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResponseBean selectByShopidAndNameAndState(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize, EmployeePageQueryDto dto) {
        dto.setPageNo(pageNo);
        dto.setPageSize(pageSize);
        PageInfo<EmployeePage> pages = employeeApiService.selectByShopidAndNameAndState(dto);
        return new ResponseBean().success(pages);
    }

    /***
     * 员工登录app系统
     * 
     * @param phone
     * @param password
     * @return
     */
    @RequestMapping(value = "/session", method = RequestMethod.POST)
    public ResponseBean EmployeeLogin(@RequestBody @Validated AppEmployeeLoginDto dto) {
        // 开发模式
        if (!isDev) {
            boolean isOk = smsManager.checkCode(dto.getPhone(), dto.getCode(), SmsTypeEnum.EMPLOYEE_LOGIN);
            if (!isOk) {
                return new ResponseBean().fail(ReturnInfo.SMS_INVALID_CODE, ReturnInfo.SMS_INVALID_MSG);
            }
        }
        AppEmployeeInfo employeeDto = employeeApiService.getEmployeeByPhoneAndPasswordAndEmail(dto.getEmail(), dto.getPhone(), dto.getPassword());
        if (employeeDto == null) {
            return new ResponseBean().fail(ReturnInfo.GET_EMPLOYEE_CODE, ReturnInfo.GET_EMPLOYEE_MESSAGE);
        }
        if (employeeDto.getIsDel() == StateEnum.ISDEL.getValue()) {
            return new ResponseBean().fail(ReturnInfo.GET_EMPLOYEE_ISDEL_CODE, ReturnInfo.GET_EMPLOYEE_ISDEL_MESSAGE);
        }

        if (employeeDto.getIsEnable() == StateEnum.DISABLE.getValue()) {
            return new ResponseBean().fail(ReturnInfo.GET_EMPLOYEE_DISABLE_CODE, ReturnInfo.GET_EMPLOYEE_DISABLE_MESSAGE);
        }

        return new ResponseBean().success(employeeDto);
    }

    /**
     * 操作员给予受益人次数
     * 
     * @param employeeId
     * @param shopId
     * @return
     */
    @RequestMapping(value = "/countEmployeeCount", method = RequestMethod.GET)
    public ResponseBean countEmployeeCount(@RequestParam("employeeId") Long employeeId, @RequestParam("shopId") Long shopId) {
        return new ResponseBean().success(employeeApiService.countEmployeeCount(employeeId, shopId));
    }

    /**
     * 操作员给予受益人人数
     * 
     * @param employeeId
     * @param shopId
     * @return
     */
    @RequestMapping(value = "/countEmployeePerson", method = RequestMethod.GET)
    public ResponseBean countEmployeePerson(@RequestParam("employeeId") Long employeeId, @RequestParam("shopId") Long shopId) {
        return new ResponseBean().success(employeeApiService.countEmployeePerson(employeeId, shopId));
    };
}
